#include "lineconnectorelement.h"
#include "../sagevis.h"
#include "../VisBaseClasses/visbaseelement.h"
#include "../VisBaseClasses/visbasewidget.h"


LineConnectorElement::LineConnectorElement(QPointer<VisBaseElement> e1, QPointer<VisBaseElement> e2, QPointer<SageVis> sageVisPointer, QPointer<VisBaseWidget> parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer, parentWidget, parent ), elt1(e1), elt2(e2)
{   
//    if( e1->getLineConnectionPoint().x() < e1->getLineConnectionPoint().x() ) //establish order based on x pos.
//    {
//        elt1 = e2;
//        elt2 = e1;
//    }

//    super::setPos(0.0, 0.0);
//    super::setGeometry(0.0, 0.0, 0.0, 0.0);

//    //-----GET LINE BOUNDARIES
//    QPointF pt1 = elt1->getLineConnectionPoint();
//    QPointF pt2 = elt2->getLineConnectionPoint();
//    if( pt1.y() < pt2.y() )
//        super::setGeometry( pt1.x(), pt1.y(), (pt2.x()-pt1.x()), (pt2.y()-pt1.y()) );
//    else
//        super::setGeometry( pt1.x(), pt2.y(), (pt2.x()-pt1.x()), (pt1.y()-pt2.y()) );

    setZValue(-10.0);

    elt1ConnectionType = DEFAULT_LINE_CONNECTION;
    elt2ConnectionType = DEFAULT_LINE_CONNECTION;
}

LineConnectorElement::LineConnectorElement(QPointer<VisBaseElement> e1, QPointer<VisBaseElement> e2, int type1, int type2, QPointer<SageVis> sageVisPointer, QPointer<VisBaseWidget> parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer, parentWidget, parent ), elt1(e1), elt2(e2)
{
//    if( e1->getLineConnectionPoint().x() < e1->getLineConnectionPoint().x() ) //establish order based on x pos.
//    {
//        elt1 = e2;
//        elt2 = e1;
//    }

//    super::setPos(0.0, 0.0);
//    super::setGeometry(0.0, 0.0, 0.0, 0.0);

//    //-----GET LINE BOUNDARIES
//    QPointF pt1 = elt1->getLineConnectionPoint();
//    QPointF pt2 = elt2->getLineConnectionPoint();
//    if( pt1.y() < pt2.y() )
//        super::setGeometry( pt1.x(), pt1.y(), (pt2.x()-pt1.x()), (pt2.y()-pt1.y()) );
//    else
//        super::setGeometry( pt1.x(), pt2.y(), (pt2.x()-pt1.x()), (pt1.y()-pt2.y()) );

    setZValue(-10.0);

    elt1ConnectionType = type1;
    elt2ConnectionType = type2;
}

QString LineConnectorElement::getUniqueId()
{
    QString e1Id = elt1->getUniqueId();
    QString e2Id = elt2->getUniqueId();
    if( e1Id.compare(e2Id ) >= 0 )
        return elt1->getUniqueId() + elt2->getUniqueId();
    else
        return elt2->getUniqueId() + elt1->getUniqueId();
}

void LineConnectorElement::updatedSlot()
{
    if( ! elt1 || ! elt2 )
    {
        return;
    }
    //-----GET LINE BOUNDARIES
    QPointF pt1;
    if( elt1ConnectionType == DEFAULT_LINE_CONNECTION )
        pt1 = elt1->getLineConnectionPoint();
    else
        pt1 = elt1->getLineConnectionPoint( elt1ConnectionType );

    QPointF pt2;
    if( elt2ConnectionType == DEFAULT_LINE_CONNECTION )
        pt2 = elt2->getLineConnectionPoint();
    else
        pt1 = elt2->getLineConnectionPoint( elt2ConnectionType );

    float width = 0.0;
    float x = 0.0;
    if( pt1.x() >= pt2.x() )
    {
        width = pt1.x() - pt2.x();
        x = pt2.x();
    }
    else
    {
        x = pt1.x();
        width = pt2.x() - pt1.x();
    }

    float height = 0.0;
    float y = 0.0;
    if( pt1.y() >= pt2.y() )
    {
        height = pt1.y() - pt2.y();
        y = pt2.y();
    }
    else
    {
        height = pt2.y() - pt1.y();
        y = pt1.y();
    }

    //setPos( (pt1.x() + pt2.x())/2.0, (pt1.y() + pt2.y())/2.0 );
    setGeometry( x-5.0, y-5.0, width+10.0, height+10.0);

    super::updatedSlot();

}

void LineConnectorElement::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if( ! elt1 || ! elt2 )
    {
        return;
    }

    //----prevent compiler warnings by declaring what I am not using---
    Q_UNUSED(widget);
    Q_UNUSED(option);

    //----SET COLOR----
    //default color light gray, white when hovering
    QColor c(200.0, 200.0, 200.0, 255.0);
    QPen pen;
    pen.setWidth(1.0);
    if( hovering )
    {
        c.setRgb(255.0, 255.0, 255.0);
        pen.setWidth(2.0);
    }
    pen.setColor(c);

    //-----GET LINE BOUNDARIES
    QPointF pt1;
    if( elt1ConnectionType == DEFAULT_LINE_CONNECTION )
        pt1= mapFromParent( elt1->getLineConnectionPoint() );
    else
        pt1= mapFromParent( elt1->getLineConnectionPoint(elt1ConnectionType) );

    QPointF pt2;
    if( elt2ConnectionType == DEFAULT_LINE_CONNECTION )
        pt2 = mapFromParent( elt2->getLineConnectionPoint() );
    else
        pt2 = mapFromParent( elt2->getLineConnectionPoint(elt2ConnectionType) );


    //----DRAW----
    painter->setBrush( c );
    painter->setPen( pen );

//    if( pt1.y() != pt2.y() )
//    {
//        painter->setPen(Qt::red);
//        if( pt1.y() < pt2.y() )
//            painter->setPen(Qt::blue);
//    }
//    else
//        painter->setPen(Qt::yellow);

    painter->drawLine(pt1, pt2);


//    painter->setBrush(Qt::NoBrush);
//    painter->drawRect(boundingRect());

//    qDebug() << " Line connector element:  x1 = " << pt1.x() << " x2 = " << pt2.x();
//    qDebug() << "   Elt1 x2: " << elt1->x()+elt1->geometry().width() << " Elt2 x1: " << elt2->x() << endl;

}
